/*****************************************************************************
Copyright 2006 Kervin L. Pierre
    EMAIL: otlkcon@adevsoft.com

This file is part of Otlkcon.

Otlkcon is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Otlkcon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License
along with Otlkcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*******************************************************************************/

#include "stdafx.h"

#include <otlkcon_prop2str.h>

otlkcon_prop2str::otlkcon_prop2str()
{
	lpAllocateBuffer = NULL;
	lpFreeBuffer = NULL;
	bInitialized = false;
	hk = NULL;
}

otlkcon_prop2str::otlkcon_prop2str(LPALLOCATEBUFFER lb, LPFREEBUFFER fb)
{
    lpAllocateBuffer = lb;
    lpFreeBuffer = fb;

    hk = otlkcon_registryInit(lpAllocateBuffer,0,"prop2str");
	bInitialized = true;
}

otlkcon_prop2str::~otlkcon_prop2str()
{
    if(NULL != hk)
    {
		otlkcon_registryDispose(hk);
    } // if
}

void otlkcon_prop2str::init(LPALLOCATEBUFFER lpAB, LPFREEBUFFER lpFB)
{
	if(!bInitialized) 
	{
		lpAllocateBuffer = lpAB;
		lpFreeBuffer = lpFB;
		bInitialized = true;

		hk = otlkcon_registryInit(lpAllocateBuffer,0,"prop2str");
	} // if
}

char * otlkcon_prop2str::get(ULONG prop)
{
    if(!bInitialized) return NULL;

	char *propStr = NULL, *retVal = NULL;

    lpAllocateBuffer(12, (LPVOID *)&propStr);
    memset(propStr,0,12);

    sprintf(propStr, "0x%0.8X", prop);

    retVal = otlkcon_registryGetStr(lpAllocateBuffer, hk, propStr);

    return retVal?retVal:propStr;
}

bool otlkcon_prop2str::get(
	ULONG prop,
	wchar_t *lpszBuffer,
	size_t cchBufferLength)
{
	wchar_t szNumericTag[12];
	swprintf(szNumericTag, L"0x%0.8X", prop);
	
	if(!otlkcon_registryGetStrW(hk, szNumericTag, lpszBuffer, cchBufferLength))
	{
		wcsncpy(lpszBuffer, szNumericTag, cchBufferLength);
	} // if

	return true;
}
